/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WeatherSystemMain;

import CommonValue.SystemTimer;
import DTO.ControlDTO;
import SensorCheckerInterface.ISensorChecker;
import SensorCheckersImpl.ManagerSensorChecker;
import MyLogging.ILog;
import DTO.SensorDTO;
import SocketBusiness.ServerListen;
import DTO.PacketDTO;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class CheckWeatherBoundary extends ServerListen {

    PacketDTO _dataToAC;
    PacketDTO _triggerToAS;
    ManagerSensorChecker msc = new ManagerSensorChecker();

    public CheckWeatherBoundary(String systemSign, int port, ControlDTO _opration, PacketDTO _dataToAC, PacketDTO _triggerToAS) {
        super(systemSign, port, _opration);
        this._dataToAC = _dataToAC;
        this._triggerToAS = _triggerToAS;
    }

    public void installNewSensorChecker(ISensorChecker isc) {
        msc.installNewSensorChecker(isc);
    }

    public final void unInstallSensorChecker(String sign) {
        msc.unInstallSensorChecker(sign);
    }

    @Override
    public void business() {
        boolean finish = false;
        do {
            try {
                Thread.sleep(SystemTimer.CollectDataSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(CheckWeatherBoundary.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if (!this.getOperation().getFlag()) {
                System.out.println(this.getSystemSign() + " DA DUNG HOAT DONG <<<<<<<<<<<<<<<<");
                continue;
            }

            Object obj = new Object();
            try {
                obj = this.receiveMessage();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CheckWeatherBoundary.class.getName()).log(Level.SEVERE, null, ex);
                continue;
            }

            if (obj == null) {
                System.out.println("packetDTO is null");
                continue;
            }

            if (!obj.getClass().equals(PacketDTO.class)) {
                System.out.println("Invalid Type Class");
                continue;
            }

            ILog.writeLog(this.getClass() + "Da nhan duoc packet", this.getSystemSign());

            System.out.println("---------------Packet gom co :");

            for (SensorDTO i : ((PacketDTO) obj).getBuffer()) {
                System.out.println("count : >>>>>>>> " + i.getSensorCount());
                if (!msc.checkBoundary(i)) {
                    System.out.println("-------->>> Thoi tiet nguy hiem ");// bao cho AS
                    synchronized (_triggerToAS) {
                        _triggerToAS.addToBuffer(i);
                    }
                }
            }

            synchronized (_dataToAC) {// goi du lieu lai, gui cho AC
                _dataToAC.addAllToBuffer(((PacketDTO) obj).getBuffer());
            }

        } while (!finish);
    }
}
